// import QS from 'qs';
import axios from 'axios';
import Token from './token.js';
import ViewUI from 'view-design'

// import app from '../main';
// console.log(app)

//import store from '@/store/index'
//import {getCookie} from '@/assets/js/cookie'
// http://hxptn8.natappfree.cc/bmt/app/api/wxUserInfo/isValidate
if (process.env.NODE_ENV == 'development') {    
    axios.defaults.baseURL = 'http://112.126.58.113:18888';
} else if (process.env.NODE_ENV == 'debug') {    
    axios.defaults.baseURL = 'https://112.126.58.113';
} else if (process.env.NODE_ENV == 'production') {   
    axios.defaults.baseURL = 'https://112.126.58.113/';
}
axios.defaults.timeout = 10000;

// axios.defaults.headers.common['Authorization'] = store.getters.getCityFn.openid;

// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
axios.defaults.headers.post['Content-Type'] = 'Content-Type:application/x-www-form-urlencoded;charset=utf-8';
axios.interceptors.request.use(function (config) {
    config.headers['CHANNEL-NO'] = "WEB";
    config.headers['BIZ-TOKEN'] = Token.getToken();
    config.headers['ACCESS-TOKEN'] = Token.getAccessToken();
    config.headers['REFRESH-TOKEN'] = Token.getRefreshToken();
    return config;
})

axios.interceptors.response.use(
    response => {       
        if (response.status === 200) {        
            
            return Promise.resolve(response);        
        } else {            
            return Promise.reject(response);        
        }    
    }, 
    error => {        
        if (error.response.status) {      
            ViewUI.Message.error(error.response.data.msg)        
            // switch (error.response.status) {                           
            //     case 404:                    
            //         ViewUI.Message.info(error.response.msg)             
            //     break;                          
            //     default:    
            //         ViewUI.Message.info('123213')    
            // }            
            return Promise.reject(error.response);        
        }       
    }
);
export function get(url, params){    
    return new Promise((resolve, reject) =>{        
        axios.get(url, {            
            params: params        
        })        
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {            
            reject(err.data)        
        })    
    });
}

export function post(url, params) {    
    return new Promise((resolve, reject) => {         
        // axios.post(url,QS.stringify(params))        
        axios.post(url,params)        
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {   
            console.log(err)         
            reject(err.data)        
        })    
    });
}

export function put(url, params) {
    return new Promise((resolve, reject) => {
        axios.put(url,params)
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}

export function remove(url, params) {
    return new Promise((resolve, reject) => {
        axios.delete(url,params)
            .then(res => {
                resolve(res.data);
            })
            .catch(err => {
                reject(err.data)
            })
    });
}